package com.yanqu.dao;

import com.yanqu.data.entity.setting.CustomGoodsBaseValueData;
import com.yanqu.jdbc.JdbcTemplateHelper;
import org.springframework.jdbc.support.rowset.SqlRowSet;

import java.sql.SQLException;
import java.sql.Types;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CustomGoodsBaseValueDataDaoImpl extends TempDataObjectDao<CustomGoodsBaseValueData> {

    public long getMaxId() {
        String sql = "select ifnull(max(dbId),0) as maxId from t_s_monitor_goods_base_value ";
        long maxId = 0;
        try {
            SqlRowSet sqlRowSet = JdbcTemplateHelper.getJdbcTemplate().queryForRowSet(sql);
            if(sqlRowSet.next()) {
                maxId = sqlRowSet.getLong("maxId");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return maxId;
    }

    public static String getCustomGoodsBaseValueDataSql = "select * from `t_s_monitor_goods_base_value`";
    public Map<Integer, Map<Integer, Map<Integer, CustomGoodsBaseValueData>>> getCustomGoodsBaseValueData() {
        Map<Integer, Map<Integer, Map<Integer, CustomGoodsBaseValueData>>> result = new ConcurrentHashMap<>();
        try {
            SqlRowSet sqlRowSet = JdbcTemplateHelper.getJdbcTemplate().queryForRowSet(getCustomGoodsBaseValueDataSql);
            while (sqlRowSet.next()) {
                CustomGoodsBaseValueData data = parse(sqlRowSet);
                if(!result.containsKey(data.getChannelId())){
                    result.put(data.getChannelId(), new ConcurrentHashMap<>());
                }
                if(!result.get(data.getChannelId()).containsKey(data.getVip())){
                    result.get(data.getChannelId()).put(data.getVip(), new ConcurrentHashMap<>());
                }
                result.get(data.getChannelId()).get(data.getVip()).put(data.getGoodsId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {

        }
        return result;
    }

    public static String updateCustomGoodsBaseValueDataSql = "update `t_s_monitor_goods_base_value` set channelId=?,goodsId=?,vip=?,maxNum=?,maxTimes=?,deleted=? where dbId=?";
    @Override
    protected boolean updateData(CustomGoodsBaseValueData data) {
        data.setBeginWriteTime();
        int[] types = new int[]{Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.BIGINT};
        Object[] values = new Object[7];
        int i = 0;
        values[i++] = data.getChannelId();
        values[i++] = data.getGoodsId();
        values[i++] = data.getVip();
        values[i++] = data.getMaxNum();
        values[i++] = data.getMaxTimes();
        values[i++] = data.getDeleted();
        values[i++] = data.getDbId();
        if (JdbcTemplateHelper.getJdbcTemplate().update(updateCustomGoodsBaseValueDataSql, values, types) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCustomGoodsBaseValueDataSql = "insert into `t_s_monitor_goods_base_value`(dbId,channelId,goodsId,vip,maxNum,maxTimes,deleted) values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CustomGoodsBaseValueData data) {
        data.setBeginWriteTime();
        int[] types = new int[]{Types.BIGINT, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.INTEGER};
        Object[] values = new Object[7];
        int i = 0;
        values[i++] = data.getDbId();
        values[i++] = data.getChannelId();
        values[i++] = data.getGoodsId();
        values[i++] = data.getVip();
        values[i++] = data.getMaxNum();
        values[i++] = data.getMaxTimes();
        values[i++] = data.getDeleted();
        if (JdbcTemplateHelper.getJdbcTemplate().update(insertCustomGoodsBaseValueDataSql, values, types) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CustomGoodsBaseValueData parseData(SqlRowSet rs) throws SQLException {
        CustomGoodsBaseValueData data = new CustomGoodsBaseValueData();
        data.setDbId(rs.getLong("dbId"));
        data.setChannelId(rs.getInt("channelId"));
        data.setGoodsId(rs.getInt("goodsId"));
        data.setVip(rs.getInt("vip"));
        data.setMaxNum(rs.getLong("maxNum"));
        data.setMaxTimes(rs.getLong("maxTimes"));
        data.setDeleted(rs.getInt("deleted"));

        return data;
    }
}
